<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="标题">
              <a-input v-model="queryParam.title" placeholder="" />
            </a-form-item>
          </a-col>
          
          <a-col :md="8" :sm="24">
            <a-form-item label="提交人">
              <a-input v-model="queryParam.creatorName" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="(!advanced && 8) || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
            >
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm">重置</a-button>
              <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'" />
              </a> -->
              <a-dropdown v-if="selectedRowKeys.length > 0">
                <a-menu slot="overlay">
                  <a-menu-item key="1" @click="batchApprove(true)">全部通过</a-menu-item>
                  <a-menu-item key="2" @click="batchApprove(false)">全部不通过</a-menu-item>
                </a-menu>
                <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /> </a-button>
              </a-dropdown>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
       
    </div>

    <s-table ref="table" size="default" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" :columns="columns" :data="loadData" @onSelect="onChange">
      <span slot="action" slot-scope="text, record">
        <a @click="handleDeal(record)">处理</a>
        <!-- <a-divider type="vertical" />
        <a @click="handleEdit(record)">处理</a> -->
        <!-- <a-dropdown>
          <a class="ant-dropdown-link"> 更多 <a-icon type="down" /> </a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a href="javascript:;">处理</a>
            </a-menu-item>
            <a-menu-item>
              <a href="javascript:;">删除</a>
            </a-menu-item>
          </a-menu>
        </a-dropdown> -->
      </span>
    </s-table>

    <!-- 审批面版 -->
    <Approval v-model="visible" :bizId="mdl.bizId" :businessUrl="mdl.bizUrl" :detailComponent="mdl.bizUrl" @ok="searchQuery"></Approval>
  </a-card>
</template>

<script>
import STable from '@/components/table/'
import ATextarea from 'ant-design-vue/es/input/TextArea'
import AInput from 'ant-design-vue/es/input/Input'
import moment from 'moment'
import axios from 'axios'
import { getTodoList } from '@/api/api'
import Approval from '@/components/Approval/Approval'

import { postAction } from '@/api/manage'
export default {
  name: 'TableList',
  components: {
    AInput,
    ATextarea,
    STable,
    Approval
  },
  data() {
    const statuMap ={
        0:'未提交',
        1:'已提交',
        2:'已完成'
    }
    return {
      url:{

      },
      visibleCreateModal: false,
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 }
      },
      form: null,
      mdl: {},

      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        title:'',
        status:0,
        waitUserId:this.$store.getters.userInfo.id
      },
      // 表头
      columns: [
        {
          title: '标题',
          align:"center",
          dataIndex: 'title',
          width: '250px',
        },
        {
          title: '待办任务',
          align:"center",
          dataIndex: 'taskName',
          width: '150px',
        },
        {
          title: '申请人',
          align:"center",
          dataIndex: 'creatorName',
          width: '150px',
        },
        {
          title: '创建时间',
          align:"center",
          dataIndex: 'createTime',
          width: '250px',
        },
        {
          title: '操作',
          align:"center",
          // dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        let params = Object.assign({},parameter, this.queryParam)
        params.title = params.title&&`*${params.title}*`
        return getTodoList(params).then(res => {
          const obj = {
            data: res.result.records||[],
            pageNo: res.result.current,
            pageSize: res.result.size,
            totalCount: res.result.total,
            totalPage: res.result.page,
            sortColumns:res.result.sortColumns
          }
          return obj
        })
      },

      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created() {},
  methods: {
    searchQuery(){
      this.$nextTick(()=>{
        this.$refs.table.refresh()
      })
    },
    /**
     * 处理
     */
    handleDeal(record) {
      this.mdl = Object.assign({}, record)
      this.visible = true
    },
    /**
     * 批量通过
     */
    batchApprove(flag) {
      let self = this
      this.$confirm({
        title: '确认审批',
        content: '是否通过选中数据?',
        onOk: function() {
          let params = {
            bizId: self.selectedRowKeys,
            dealResult: flag?1:0
          }
          postAction('/workflow/batchSubmitTask', params).then(res => {
            if (res.success) {
              self.$message.success(res.message)
            } else {
              self.$message.warning(res.message)
            }
            self.searchQuery()
            self.onClearSelected()
          })
        }
      })
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
    onClearSelected() {
      this.$nextTick(()=>{
        this.selectedRowKeys = [];
        this.selectionRows = [];
      })
    },
    //添加逻辑
    handleModalVisible(isVisible) {
      this.visibleCreateModal = isVisible
    },

    onChange(row) {
      this.selectedRowKeys = row.selectedRowKeys
      this.selectedRows = row.selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },

    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date()),
        waitUserId:this.$store.getters.userInfo.id
      }
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
